//
//  busController.m
//  ElektroM
//
//  Created by Michael on 09.10.09.
//  Copyright 2009 Artgruppe inc. All rights reserved.
//

#import "busController.h"
#import "dataBase.h"
#import "AtomicElementViewController.h"
#import "Train.h"
#import "showInfoBusesController.h"
#import "helpForBuses.h"


@implementation busController

@synthesize databaseName;
@synthesize table, searchBar;
@synthesize infoController;


// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {		
				
    }
	
    return self;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self startSearch];	
}

-(void) loadStuff
{
	if(_db==nil)
	{		
		_db=[dataBase alloc];		
		_db.databaseName=@"Buses.sqlite";
	}
	
	[_db initialization];	
	
	_aiBusNames=[_db readBusesInfoBySearchAsNSArray: _strSearchText];
	_aiBusInfos=[_db getBusesInfoAsNSArray];
	_aiBusTimes=[_db getBusesTimesAsNSArray];	
	
	[busy stopAnimating];
}

-(void) releaseStuff
{
	[_db releaseBusInfoResources];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	_db=[dataBase alloc];		
	_db.databaseName=@"Buses.sqlite";
	
	_strSearchText=@"";	
	
	// Create a final modal view controller
	UIButton* modalViewButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
	[modalViewButton addTarget:self action:@selector(modalViewAction) forControlEvents:UIControlEventTouchUpInside];
	UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton];
	self.navigationItem.leftBarButtonItem = modalBarButtonItem;
	[modalBarButtonItem release];		
}

-(void) showLoadingIndicator
{
	[busy startAnimating];
}

-(IBAction) modalViewAction
{
	if(helpDialog==nil)
	{		
		helpDialog = [[helpForBuses alloc] initWithNibName: @"helpForBuses" bundle:nil];		
	}
	
	_bLoadChild = true;	
	[self.navigationController presentModalViewController: helpDialog animated:YES];
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
	if(_strSearchText!=nil)
	{
		[_strSearchText release];
		_strSearchText=nil;
	}
	_strSearchText=[NSString stringWithString: searchText];
	[_strSearchText retain];
}

-(void) startSearch
{	
	[_db initialization];
	
	_aiBusNames=[_db readBusesInfoBySearchAsNSArray: _strSearchText];
	_aiBusInfos=[_db getBusesInfoAsNSArray];
	_aiBusTimes=[_db getBusesTimesAsNSArray];
	
	[table reloadData];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
	
	[_db initialization];
	// initialize that stuff as the new:	
	_aiBusNames=[_db readBusesInfoBySearchAsNSArray: @""];
	_aiBusInfos=[_db getBusesInfoAsNSArray];
	_aiBusTimes=[_db getBusesTimesAsNSArray];
	
	[table reloadData];	
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	// I do not know why Interface Editor didn't make that for us,
	// so do it manually:
	if(table != tableView)
		table=tableView;
	
    return [_aiBusNames count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *kCellIdentifier = @"cellID";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
	if (!cell)
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCellIdentifier] autorelease];
        
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        cell.textLabel.backgroundColor = [UIColor clearColor];
		cell.textLabel.opaque = NO;
		cell.textLabel.textColor = [UIColor blackColor];
		cell.textLabel.highlightedTextColor = [UIColor whiteColor];
		cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
		
		cell.detailTextLabel.backgroundColor = [UIColor clearColor];
		cell.detailTextLabel.opaque = NO;
		cell.detailTextLabel.textColor = [UIColor grayColor];
		cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
		cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
    }	

	cell.textLabel.text = [_aiBusNames objectAtIndex: indexPath.row];	
	cell.detailTextLabel.text = @"";
	
	return cell;
}

#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {	
	if(self.infoController!=nil)
	{
		//[infoController release];
		self.infoController=nil;		
	}
	showInfoBusesController* contr=[[showInfoBusesController alloc] init];
	self.infoController=contr;
	[contr release];
	
	infoController.strInfo = [_aiBusInfos objectAtIndex: indexPath.row];
	infoController.strTime= [_aiBusTimes objectAtIndex: indexPath.row];
	infoController.strTitle= [_aiBusNames objectAtIndex: indexPath.row];
	
	_bLoadChild = true;
	
	[self.navigationController pushViewController:infoController animated:YES];			
} 
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 
*/
- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewWillAppear:(BOOL)animated{
	if(!_bLoadChild)
		[self loadStuff];	
	
	_bLoadChild = false;
	//[busy stopAnimating];
}

- (void)viewWillDisappear:(BOOL)animated{		
	if(!_bLoadChild)
		[self releaseStuff];
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;	
	[databaseName release];
	databaseName=nil;
	
	if(self.infoController!=nil)
	{	
		self.infoController=nil;		
	}

	if(_db!=nil){
	    _db.namePrefix=nil;
		_db.databaseName=nil;
		[_db releaseBusInfoResources];
	    [_db release];
	    _db = nil;
	}
}

- (void)dealloc {
    [super dealloc];
}


@end
